package com.kw.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kw.drug.common.ResultMapUtil;
import com.kw.drug.entity.Druginfo;
import com.kw.drug.entity.Problem;
import com.kw.drug.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 问题药品相关的controller
 */
@Controller
@RequestMapping(value = "/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    /**
     * 转向问题药品页面
     */
    @RequestMapping
    public String problem() {
        return "problem";
    }


    /**
     * 转向问题药品编辑页面
     */
    @RequestMapping(value = "/problemQueryById")
    public String problemQueryById(@RequestParam(name = "id", required = true) Integer id, Model model) {
        Problem problem = problemService.queryProblemById(id);
        model.addAttribute("obj", problem);
        return "problemPage";

    }

    /**
     * 删除问题商品
     */
    @RequestMapping("/problemDelById")
    @ResponseBody
    public Map problemDelById(Integer id) {
        try {
            Integer integer = problemService.problemDelById(id);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }


    }

    /**
     * 添加问题商品页面
     */
    @RequestMapping("/problemPage")

    public String addIndex() {
        return "problemPage";


    }

    /**
     * 添加问题商品
     */

    @RequestMapping("/problemAdd")
    @ResponseBody
    public Map problemPage(Problem problem) {
        problem.setCreatetime(new Date());
        try {
            Integer integer = problemService.addProblem(problem);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }

    }

    /**
     * 分页查询药品出入库列表
     */
    @RequestMapping(value = "/problemQueryPage")
    @ResponseBody
    public Map owinfoQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<Problem> ipage = problemService.problemQueryPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(ipage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
}


